﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace Macaw.Wss3.FieldContentPanelSolution
{
    public abstract class EmptyFieldValueHelper
    {
        /// <summary>
        /// This method will check if the given field with the fieldvalue is "empty"
        /// </summary>
        /// <param name="field">The field-definition</param>
        /// <param name="fieldvalue">The fieldvalue</param>
        /// <returns>"True" if empty</returns>
        public static bool FieldEmpty(SPField field, object fieldvalue)
        {
            if (fieldvalue == null)
            {
                return true;
            }
            else if (fieldvalue is SPFieldLookupValue)
            {
                return false;
            }
            else if (fieldvalue is SPFieldMultiChoiceValue)
            {
                SPFieldMultiChoiceValue typedvalue = (SPFieldMultiChoiceValue)fieldvalue;
                return (typedvalue.Count == 0) ? true : false;
            }
            else if (fieldvalue is SPFieldMultiColumnValue)
            {
                SPFieldMultiColumnValue typedvalue = (SPFieldMultiColumnValue)fieldvalue;
                return (typedvalue.Count == 0) ? true : false;
            }
            else if (fieldvalue is SPFieldRatingScaleValue)
            {
                SPFieldRatingScaleValue typedvalue = (SPFieldRatingScaleValue)fieldvalue;
                return (typedvalue.Count == 0) ? true : false;
            }
            else if (fieldvalue is SPFieldUrlValue)
            {
                SPFieldUrlValue typedvalue = (SPFieldUrlValue)fieldvalue;
                return string.IsNullOrEmpty(typedvalue.Url);
            }
            else if (fieldvalue is SPFieldUserValue)
            {
                return false;
            }
            else if (fieldvalue is string)
            {
                string typedvalue = ((string)fieldvalue).Trim();
                return string.IsNullOrEmpty(typedvalue);
            }
            else
            {
                // There is apparently a FieldValue object instance, but no FieldValueType-specific-check was implemented.
                // As there is a value, return "False" 
                return false;
            }
        }

    }
}
